#include "draw_helper.h"

inline int bit(int a, int b)
{
	return (a & (1 << b)) >> b;
}

unsigned int duIntToCol(int i, int a)
{
	int	r = bit(i, 1) + bit(i, 3) * 2 + 1;
	int	g = bit(i, 2) + bit(i, 4) * 2 + 1;
	int	b = bit(i, 0) + bit(i, 5) * 2 + 1;
	return duRGBA(r * 63, g * 63, b * 63, a);
}

void duIntToCol(int i, float* col)
{
	int	r = bit(i, 0) + bit(i, 3) * 2 + 1;
	int	g = bit(i, 1) + bit(i, 4) * 2 + 1;
	int	b = bit(i, 2) + bit(i, 5) * 2 + 1;
	col[0] = 1 - r * 63.0f / 255.0f;
	col[1] = 1 - g * 63.0f / 255.0f;
	col[2] = 1 - b * 63.0f / 255.0f;
}

unsigned int areaToCol(unsigned int area)
{
	if (area == 0)
	{
		// Treat zero area type as default.
		return duRGBA(0, 192, 255, 255);
	}
	else
	{
		return duIntToCol(area, 255);
	}
}

void dd_vertex(const fixed64& vx, const fixed64& vy, const fixed64& vz,unsigned int c, FPRaw* vp, int& vlen, unsigned int* cp, int& clen, int vmax, int cmax)
{
	if (vp && cp && vlen + 3 <= vmax && clen + 1 <= cmax)
	{
		vp[vlen++] = vx;
		vp[vlen++] = vy;
		vp[vlen++] = vz;
		vlen += 3;
		cp[clen] = c;
		++clen;
	}
}